package com.yjt.termworks.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yjt.termworks.MainActivity;
import com.yjt.termworks.R;
import com.yjt.termworks.ServiceActivity;
import com.yjt.termworks.adapter.HomeCircleAdapter;
import com.yjt.termworks.entity.HomeCircle;
import com.yjt.termworks.entity.User;
import com.yjt.termworks.sql.MyHelper;
import com.yjt.termworks.utils.ImageLoaderUtils;
import com.yjt.termworks.utils.SharePreferenceData;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ServiceFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ServiceFragment extends Fragment {

    // 通讯录数据
    static View view = null;
    private RecyclerView mRecyclerview;
    private HomeAdapter mAdapter;
    private static String[] names={"班主任","数学老师","英语老师","家长","家长2"};
    private int[] icons={R.drawable.avatar,R.drawable.avatar,R.drawable.avatar,R.drawable.avatar,R.drawable.avatar};
    private String[] introduces={
            "法撒旦撒阿德飒飒",
            "dasdasdas dasd",
            "sadfsafdwqeasd",
            "sadasdasdasdasd",
            "法撒旦撒阿德飒飒",
    };
    private String baseImageUrl = "http://10.175.253.209:9002/images/userAvatar/";

//    private String baseImageUrl=R.string.baseImageUrl+"";
   static ArrayList<User> users;
    // 通讯录


    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public ServiceFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ServiceFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ServiceFragment newInstance(String param1, String param2) {
        ServiceFragment fragment = new ServiceFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_service, container, false);
        mRecyclerview = view.findViewById(R.id.id_recyclerview);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(view.getContext()));
        // 设置页面排列为垂直方向
        mAdapter = new HomeAdapter();
        mRecyclerview.setAdapter(mAdapter);

        getSQLData();

        mAdapter.setOnItemClickListener(new HomeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
//                Toast.makeText(view.getContext(),"跳转"+position,Toast.LENGTH_SHORT).show();

                // 携带数据
                MyHelper myHelper = new MyHelper(view.getContext());
                SQLiteDatabase db = myHelper.getWritableDatabase();
                int id = position;
                id++;
                Cursor cursor1 = db.rawQuery("select * from USER where _id = ?", new String[]{id+""});
                if(cursor1.moveToFirst()){
                     String phone =  cursor1.getString(2);
                    Boolean flag = SharePreferenceData.saveUserData2(view.getContext(),phone);
                }

                Intent intent = new Intent(view.getContext(), MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("flag", 4);
                startActivity(intent);
            }
        });
        // Inflate the layout for this fragment
        return view;
    }

    private void getSQLData(){

        MyHelper myHelper = new MyHelper(view.getContext());
        SQLiteDatabase db = myHelper.getWritableDatabase();
        Cursor cursor = db.query("USER", null, null, null, null, null, null);
        if(cursor.getCount() == 0){
            Toast.makeText(view.getContext(),"没有数据",Toast.LENGTH_SHORT).show();
            return;
        }
        users = new ArrayList<>();
        User ci = null;

        while (cursor.moveToNext()){
            ci = new User();
            ci.setName(cursor.getString(1));
            ci.setPhone(cursor.getString(2));
            ci.setAvatar(baseImageUrl+cursor.getString(4));
            users.add(ci);
        }
        cursor.close();
        db.close();

    }


    static class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
        private OnItemClickListener mOnItemClickListener;

        @Override
        public HomeAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//            HomeAdapter.MyViewHolder holder =new HomeAdapter.MyViewHolder(LayoutInflater.from(view).inflate(R.layout.recycler_item,parent,false));
//            return holder;
//            return new MyViewHolder(view);
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            View itemView = inflater.inflate(R.layout.recycler_item, parent, false); // 将第二个参数 attachToRoot 设置为 false
            return new MyViewHolder(itemView);
        }

        public interface OnItemClickListener{
            void onItemClick(View view,int position);
        }

        public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
            this.mOnItemClickListener = mOnItemClickListener;
        }



        @Override
        public void onBindViewHolder(@NonNull HomeAdapter.MyViewHolder holder, int position) {

            if(mOnItemClickListener != null){
                //为ItemView设置监听器
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition(); // 1
                        mOnItemClickListener.onItemClick(holder.itemView,position); // 2
                    }
                });
            }

            holder.name.setText(users.get(position).getName());
//            holder.touxiang.setImageResource(icons[position]);
            ImageLoaderUtils.display(view.getContext(),holder.touxiang,users.get(position).getAvatar());
            holder.introduce.setText(users.get(position).getPhone());
        }

        @Override
        public int getItemCount() {
            return names.length;
        }
        class MyViewHolder extends RecyclerView.ViewHolder{
            TextView name;
            ImageView touxiang;
            TextView introduce;
            public MyViewHolder(View view){
                super(view);
                name=view.findViewById(R.id.name);
                touxiang=view.findViewById(R.id.touxiang);
                introduce=view.findViewById(R.id.introduce);
            }
        }


    }
}